<template>
  <div class="role">
    <el-button icon="el-icon-plus" @click="openDialog()">添加</el-button>
    <el-table :data="tableData">
      <el-table-column label="角色" prop="name" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button icon="el-icon-edit" @click="openDialog1(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="权限设置" :visible.sync="dialogVisible" width="50%" @closed="closeDialog">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="名称：" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="权限菜单：">
          <el-checkbox-group v-model="form.privileges">
            <el-checkbox
              v-for="(item,index) in privilegeList"
              :key="index"
              :label="item.id"
            >{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  deleteRole,
  getPrivilegeList,
  setRole,
  addRole
} from '@/api/privilege'

export default {
  name: 'Role',
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      privilegeList: [],
      form: {
        privileges: []
      },
      userId: '',
      rules: {
        // name: [{ required: true, message: '请输入名称', trigger: 'change' }]
      }
    }
  },
  mounted() {
    this.getList()
    this.userId = localStorage.getItem('userid')
    console.log(this.userId, 23)
  },
  methods: {
    getList() {
      getRoleList({}).then((res) => {
        console.log(111, res.data.records)
        this.tableData = res.data.records
      })
    },
    getPrivilegeList() {
      getPrivilegeList({ userId: this.userId }).then((res) => {
        this.privilegeList = res.data
      })
    },
    getPrivilegeList1(row) {
      getPrivilegeList({ userId: this.userId }).then((res) => {
        this.privilegeList = res.data
        if (row.privilegeIds) {
          this.form.privileges.push(...row.privilegeIds)
        }
        // console.log(this.form.privileges,2222)

        // console.log(row.privilegeIds.split(','),2)
      })
    },
    openDialog() {
      this.getPrivilegeList()
      // console.log(row.privileges)
      // let arr=[1,3]
      // if (row) {
      //   this.form = Object.assign({}, row, {
      //     privileges: arr.map((item) => +item)
      //   })
      // }
      this.dialogVisible = true
    },
    openDialog1(row) {
      console.log(row)
      this.form.name = row.name
      this.form.id = row.id

      this.getPrivilegeList1(row)
      // console.log(row.privileges)
      // let arr=[1,3]
      // console.log(row,1111)
      // console.log(this.privilegeList)
      // if (row) {
      //   this.form = Object.assign({}, row, {
      //     privileges: row.privileges.map((item) => +item)
      //   })
      // }
      this.dialogVisible = true
    },
    closeDialog(row) {
      this.dialogVisible = false
      setTimeout(() => {
        this.$refs.form.clearValidate()
      }, 0)
      this.form = {
        privileges: []
      }
    },
    deleteRow({ id }) {
      this.$confirm('是否删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteRole({ roleId: id }).then((res) => {
            if (res.code === 0) {
              this.$notify.success({
                title: '成功',
                message: '删除成功'
              })
              this.getList()
            }
          })
        })
        .catch(() => {})
    },
    submit() {
      console.log(this.form)
      if (this.form.name == '') {
        this.$message.error('请输入名称')
        return
      }

      console.log()
      const { id, name, privileges } = this.form
      console.log(this.form)
      // return;
      // privileges
      for (let i = 0; i < privileges.length; i++) {
        privileges[i] = parseInt(privileges[i])
      }
      const params = { privilegeIds: [...privileges], name }
      const promise = id
        ? setRole({ ...params, id })
        : addRole(params)
      promise.then((res) => {
        if (res.code === 0) {
          this.$notify.success({
            title: '成功',
            message: id ? '修改角色权限成功' : '新增角色权限成功'
          })
          this.closeDialog()
          this.getList()
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
